#!/bin/sh

# $Id: run_program,v 1.17 2009-07-12 00:42:40 brian Exp $
# Copyright (c)2007 Brian Manning <brian at portaboom dot com>

# PLEASE DO NOT E-MAIL THE AUTHOR ABOUT THIS SOFTWARE
# The proper venue for questions is the LACK mailing list at:
# http://groups.google.com/linuxack or <linuxack.googlegroups.com>

if ! [ -e $LACK_FUNCTIONS ]; then
    LACK_FUNCTIONS="/etc/scripts/lack_functions.sh"
fi # if ! [ -e $LACK_FUNCTIONS ]
source $LACK_FUNCTIONS

ACTION=$1
BASENAME="run-program"
DESC="Running special run requests from /proc/cmdline"

case "$ACTION" in
  vars)
    echo "${BASENAME}:"
    echo "cmdline - run; Runs either a shell, init, or the installer"
    echo "comment - (run=sh, run=init, run=install)"
    exit 0
  ;;
  start)
    colorize_nl $S_TIP "$BASENAME: $DESC"
    colorize_nl $S_INFO "$BASENAME: PID is ${$}"
    file_parse "/proc/cmdline" "run" # returns any parsed data as $PARSED
    RUN_PROG=$PARSED

    # verify 'run=' was called for; if it wasn't called for, don't try to run
    # anything below, just skip out of this script and let the scripts past
    # this one do their thing
    if [ "x$RUN_PROG" != "x" ]; then
        case "${RUN_PROG}" in
            sh)
                colorize_nl $S_INFO "$BASENAME: run=sh; running a shell";
                #exec /bin/sh < dev/console > dev/console 2>&1;;
                #echo -n "Running a shell; "
                echo "hit Ctrl-D or type 'exit' to continue booting"
                exec $BB sh
                ;;
            init)
                colorize_nl $S_INFO "$BASENAME: run=init; running busybox init";
                if [ $$ -eq 1 ]; then
                    exec $BB init < dev/console > dev/console 2>&1
                else
                    colorize_nl $S_FAILURE \
                        "$BASENAME: run=init specified but PID != 1 (PID $$)";
                    colorize_nl $S_FAILURE \
                        "$BASENAME: (i.e. script not exec'ed from /init)";
                    colorize_nl $S_INFO \
                        "$BASENAME: exec'ing busybox shell (/bin/sh)";
                    #exec $BB sh < dev/console > dev/console 2>&1
                    exec $BB sh
                fi # if [ $PPID -eq 1 ]
                ;;
            install)
                colorize_nl $S_INFO "$BASENAME: run=install; running installer";
                exec $BB sh /etc/install.sh
                ;;
            *)
                colorize_nl $S_TIP "$BASENAME: unknown 'run': ${RUN_PROG}";
                colorize_nl $S_TIP "$BASENAME: starting busybox ash shell";
                exec $BB sh
                ;;
        esac # case "${RUN_PROG}"
    fi # if [ "x$RUN_PROG" != "x" ]
    ;; # start)
  stop)
    # noop
    :
    ;; # stop)
  restart|force-reload)
    # noop
    :
    ;;
  *)
    echo "Usage: $BASENAME {start|stop|restart|force-reload}" >&2
    exit 3
    ;;
esac

exit 0
# vi: set shiftwidth=4 tabstop=4 filetype=sh :
# конец!
